package com.yxj.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {

    private Logger logger = LoggerFactory.getLogger(MyAdvice.class);

    //定义切面
    @Pointcut("execution(* com.yxj.controller.*.*(..))")
    public void myPoint() {
    }

    @Before("myPoint()")
    public void before(){
        System.out.println("before....");
    }
    //环绕通知
    @Around("myPoint()")
    public Object myLog(ProceedingJoinPoint pjp) throws Throwable {
        //获取类名
        String className = pjp.getTarget().getClass().toString();
        //获取方法名
        String methodName = pjp.getSignature().getName();
        //获取参数
        Object[] args = pjp.getArgs();

        ObjectMapper mapper = new ObjectMapper();

        logger.info("类名:"+className+"\r\n"+"方法名:"+methodName+"\r\n"+"参数:"+mapper.writeValueAsString(args));
        //执行目标方法
        Object proceed = pjp.proceed();
        logger.info("返回值："+mapper.writeValueAsString(proceed));

        return proceed;
    }

}

